/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.infrastructure;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 文件工具类
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class FileUtil {

	private static Log logger = LogFactory.getLog(FileUtil.class);

	// 这便是读取桌面路径的方法了

	public static File getHomeDirectory() {
		javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView
				.getFileSystemView();

		return fsv.getHomeDirectory();
	}

	public static File createDire(String filePath) {
		File direFile = new File(filePath);
		if (!direFile.exists()) {
			direFile.mkdir();
		}
		return direFile;
	}

	public static File createFileIfNotExsit(String filePath) {
		File file = new File(filePath);
		return createFileIfNotExsit(file);
	}

	public static File createFileIfNotExsit(File file) {
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException ex) {
				Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return file;
	}

	public static File createDireIfNotExsit(File file) {
		if (!file.exists()) {
			file.mkdir();
		}
		return file;
	}

	public static String readTxtFile(File file) {
		StringBuffer strBuffer = new StringBuffer();
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(
					file), "GB2312");
			BufferedReader br = new BufferedReader(isr);

			String strLine = null;
			while ((strLine = br.readLine()) != null) {
				strBuffer.append(strLine).append(Config.LINE_SPLITER);
			}
			br.close();
			isr.close();

		} catch (Exception ex) {
			ex.printStackTrace();
			;
		}
		return strBuffer.toString()
				.replaceAll(Config.LINE_SPLITER_REPLACE, "\r\n")
				.replaceAll(Config.LINE_SPLITER_REPLACE_TWO, "\n");
	}

	public static void copyFile(File f1, String fileStr2) {
		try {

			File f2 = createFileIfNotExsit(fileStr2);

			int length = 2097152;
			FileInputStream in = new FileInputStream(f1);
			FileOutputStream out = new FileOutputStream(f2);
			byte[] buffer = new byte[length];
			int ins = -1;
			while (-1 != (ins = in.read(buffer))) {

				out.write(buffer, 0, ins);
			}
			in.close();
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 为了在txt中以\r\n区分一行，所以必须去掉里面除了换行外所有的\r\n
	 * 
	 * @param file
	 * @param str
	 */
	public static void writeStrToTxtFileReplaceIfExsit(File file, String str) {
		if (str == null || str.trim().equals("")) {
			return;
		}
		// if (file.exists()) {
		// FileUtil.copyFile(file, file.getAbsolutePath() + "-bak.txt");
		// }

		try {
			if (file.exists()) {
				file.delete();
			}
			file.createNewFile();
			OutputStreamWriter osw = new OutputStreamWriter(
					new FileOutputStream(file), "GB2312");
			BufferedWriter bw = new BufferedWriter(osw);
			str = str.replaceAll("\r\n", Config.LINE_SPLITER_REPLACE)
					.replaceAll("\n", Config.LINE_SPLITER_REPLACE_TWO)
					.replaceAll(Config.LINE_SPLITER, "\r\n");
			bw.write(str);
			bw.close();
			osw.close();

		} catch (Exception ex) {
			logger.error("file:" + file.getAbsolutePath());
			ex.printStackTrace();
		}
	}

	/**
	 * 为了在txt中以\r\n区分一行，所以必须去掉里面除了换行外所有的\r\n
	 * 
	 * @param file
	 * @param str
	 */
	public static void WriteStrToFile(File file, String str) {
		if (str == null || str.trim().equals("")) {
			return;
		}

		try {
			OutputStreamWriter osw = new OutputStreamWriter(
					new FileOutputStream(file), "GB2312");
			BufferedWriter bw = new BufferedWriter(osw);
			bw.write(str);
			bw.close();
			osw.close();

		} catch (Exception ex) {
			logger.error("file:" + file.getAbsolutePath());
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		StringBuffer strBuffer = new StringBuffer();
		try {
			InputStreamReader isr = new InputStreamReader(
					new FileInputStream(
							new File(
									"C:\\Documents and Settings\\hp\\goodEnglisht\\zimuWord\\吸血鬼.txt.txt")),
					"GB2312");
			BufferedReader br = new BufferedReader(isr);

			String strLine = null;
			while ((strLine = br.readLine()) != null) {
				System.out.println(strLine);
				strBuffer.append(strLine).append(Config.LINE_SPLITER);
			}
			br.close();
			isr.close();

		} catch (Exception ex) {
			ex.printStackTrace();
			;
		}
	}
}
